package com.quanquan.store.controller;

import com.quanquan.store.dto.AdsenceDTO;
import com.quanquan.store.request.AdsenceRequest;
import com.quanquan.store.service.AdsenceService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;

/**
 * @ClassName:Adsence
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/18 18:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/adsence")
public class AdsenceController {

    private final AdsenceService adsenceService;

    @Autowired
    public AdsenceController(AdsenceService adsenceService) {
        this.adsenceService = adsenceService;
    }

    /**
     * 添加广告位
     *
     * @param request
     * @return store
     */
    @PostMapping("/add_adsence")
    public ResultBody addAdsence(@RequestBody AdsenceRequest request) {
        if (StringUtils.isEmpty(request.getName())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "广告位信息不完整");
        }
        boolean flag = adsenceService.saveAdsence(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }


    /**
     * 删除广告位
     *
     * @param request
     * @return
     */
    @PostMapping("/remove_adsence")
    public ResultBody removeAdsence(@RequestBody AdsenceRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明要删除的广告位id");
        }
        boolean flag = adsenceService.removeAdsense(request.getId());
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
    }


    /**
     * 修改广告位信息
     *
     * @param request
     * @return
     */
    @PostMapping("update_adsence")
    public ResultBody updateAdsence(@RequestBody AdsenceRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明id");
        }
        boolean flag = adsenceService.updateAdsense(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
    }

    /**
     * 查找广告位列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list")
    public ResultBody queryList(@RequestBody AdsenceRequest request) {
        List<AdsenceDTO> adsenceDTOList = adsenceService.queryList(request);
        if (adsenceDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无广告位列表");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", adsenceDTOList);
    }

    /**
     * 广告位详情
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    public ResultBody queryById(@RequestBody AdsenceRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的广告位id为空");
        }
        AdsenceDTO adsenceDTO = adsenceService.query(request.getId());
        if (adsenceDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此广告位信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", adsenceDTO);
    }


}
